const socket = new WebSocket('ws:127.0.0.1:9998')

socket.onopen = ()=>{
  console.log("websocket服务已连接");
}

socket.onerror = ()=>{
  console.log("出错了");
}

socket.onclose = ()=>{
  console.log("服务已断开");
}
// 监听是否收到消息
export const listenMsg = (fn) =>{
  socket.onmessage = fn
}

// 发送消息
// data:{hasNewMsg:true,chatRoomId}
export const sendMsg = data =>{
  data = JSON.stringify(data)
  socket.send(data)
}

// 关闭连接
export const closeSocket = ()=>{
  socket.close()
  console.log('websocket服务已断开');
}